package utils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

class Person{
	int age;
	int score;
	public Person(int age, int score) {
		super();
		this.age = age;
		this.score = score;
	}
}

public class Others {
	@Test
	public void scanner() {
		Scanner in = new Scanner(System.in);
		//1.输入一个整数
		in.nextByte();
		in.nextShort();
		in.nextInt();
		in.nextLong();
		in.nextFloat();
		in.nextDouble();
		in.nextBoolean();
		in.nextBigInteger();
		in.nextBigDecimal();
		// 遇到空白断开输入
		in.next();
		// 遇到回车断开输入
		in.nextLine();
		in.close();
	}

	@Test
	public void random() {
		Random rand = new Random();
		//1.随机数
		System.out.println(rand.nextInt());
		System.out.println(rand.nextLong());
		System.out.println(rand.nextFloat());
		System.out.println(rand.nextDouble());
		// 10以内 0~9 
		System.out.println(rand.nextInt(10));
		// 随机生成：58~69 : rand.nextInt(max-min+1) + min
		System.out.println(rand.nextInt(69-58+1) + 58);
		// 2.随机布尔
		System.out.println(rand.nextBoolean());
		// 3.随机生成byte[]
		byte[] arr = new byte[100];
		rand.nextBytes(arr);
		System.out.println(Arrays.toString(arr));
	}

	@Test
	public void objects() {
		// 1.比较对象是否相等 ， 如果要比较自定义类对象，需要重写equals方法（不重写就调用继承来的==比较）
		boolean equals = Objects.equals(null, null);
		System.out.println(equals);
		// 2.判断是否空，null->true 
		Objects.isNull("aaa");
		//   判断是否不为空  !null ->true
		Objects.nonNull("abc");
		// 3.比较对象大小
		Person p1 = new Person(11, 90);
		Person p2 = new Person(11, 70);
		// 相等返回0，前大正，后大负数
		int compare = Objects.compare(p1,p2, new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o1.age - o2.age;
			}
		});
		System.out.println(compare);
		
		String s = "abcdefg";
		StringBuilder sb = new StringBuilder(s);
		sb.reverse();
		String s2 = sb.toString();
	}
	
	
	public static String toString(int[] arr) {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		
		for(int i = 0 ; i < arr.length; i++) {
			sb.append(arr[i]);
			if (i != arr.length - 1) {
				sb.append(",");
			}
		}
		
		sb.append("]");
		
		return sb.toString();
	}
	
	@Test
	public void arrays() {
		int[] arr = {1,2,4};
		System.out.println(arr);  // [I@14514713
		boolean[] arr2 = {true,false,true};
		System.out.println(arr2);  // [Z@69663380
		char[] arr3 = {'a','b','c'};
		System.out.println(arr3);  // 只不过char[] 重写了toString()
		
		// 1.把数组转成字符串
		System.out.println(Arrays.toString(arr));
		// 2.填充数组 
		int[] arr4 = new int[5];
		Arrays.fill(arr4, 100);
		System.out.println(Arrays.toString(arr4));
		// 3.排序
		int[] arr5 = {1,52,12,1643,121,11};
		Arrays.sort(arr5);   //默认的从小到大 [1, 11, 12, 52, 121, 1643]
		System.out.println(Arrays.toString(arr5));
		// 4.二分法查找 : 要求!!!数组一定是排好序的数组
		int index = Arrays.binarySearch(arr5, 111);
		System.out.println(index);
		// 5.数组拷贝   , (arr5全部拷贝，新数组长度)
		int[] arr6 = Arrays.copyOf(arr5, 20);
		System.out.println(Arrays.toString(arr6));
		// 指定拷贝位置和个数
		// System.arraycopy(arr5, 0, arr6, 3, 3);
	}
}









